﻿using System;
using System.Collections.Generic;
using System.IO.IsolatedStorage;
using System.Windows.Input;
using DDU.Library;
using System.Linq;

namespace DDU.ViewModel
{
    public class PreferenceViewModel : ViewModelBase
    {
        public bool ShowUnlockButton
        {
            get { return ConstKeys.Edition == Enums.EditionValue.Standard && GlobalSettings.AdvancedLocked; }
        }

        public IEnumerable<string> SupportLanguages
        {
            get
            {
                yield return "zh-CN";
                yield return "en-US";
                yield return "zh-HK";
                yield return "zh-TW";
            }
        }

        public ICommand CleanBookCoverCommand
        {
            get
            {
                return new GenericCommand()
                {
                    ExecuteCallback = delegate
                    {
                        CleanCoverImages();
                    }
                };
            }
        }

        public ICommand MakeZeroCommand
        {
            get
            {
                return new GenericCommand()
                {
                    ExecuteCallback = delegate
                    {
                        GlobalSettings.WiFiNetworkDataLength = 0;
                        GlobalSettings.CellNetworkDataLength = 0;
                        GlobalSettings.Save();
                    }
                };
            }
        }

        private void CleanCoverImages()
        {
            IEnumerable<string> images = Database.CollectionOfBooks.Select(b => b.Image);
            if (images.Count() > 0)
            {
                using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    string[] a = images.ToArray();
                    string[] files = store.GetFileNames(ConstKeys.BookDirectoryName + "\\*.*");
                    if (files.Length - a.Length == 0)
                    {
                        return;
                    }
                    foreach (string file in files)
                    {
                        string afile = ConstKeys.BookDirectoryName + "\\" + file;
                        if (!images.Contains(afile))
                        {
                            store.DeleteFile(afile);
                        }
                    }
                }
            }
        }
    }
}
